package com.jdz.noise.controller;


import com.jdz.noise.dao.FlyStatusDao;
import com.jdz.noise.dao.NoiseDao;
import com.jdz.noise.entities.FlyStatus;
import com.jdz.noise.entities.SyncRow;
import com.jdz.noise.service.NoiseService;
import com.jdz.noise.service.impl.NoiseServiceImpl;
import com.jdz.noise.tools.ControlUtil;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.io.IOException;

import static com.jdz.noise.tools.ControlUtil.*;
@RestController
public class DataUploadController {
    @Autowired
    private NoiseServiceImpl noiseServiceImpl;
    @Autowired
    private FlyStatusDao flyStatusDao;
//    @RequestMapping("/uploadNoise")
//    public String uploadNoise(@RequestParam("file") MultipartFile file) {
//        System.out.println("上传噪声数据:"+file.getOriginalFilename());
//        System.out.println("ContentType:"+file.getContentType());
//        String pathString=ControlUtil.uploadFile(file,NOISE_TYPE);
//        //另外开启一个线程异步执行数据的保存
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    noiseServiceImpl.putNoiseFileDataToSync(pathString);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//        return "{\"code\":0, \"msg\":\"success\", \"fileUrl\":\"" + pathString + "\"}";
//
//    }


    @RequestMapping("/uploadNoise")
    @ResponseBody
    public String uploadNoise(@RequestParam("file") MultipartFile file) throws Exception {
        list_noise.clear();
        //
        System.out.println("上传噪声数据:"+file.getOriginalFilename());
        String res=ControlUtil.uploadFile(file,NOISE_TYPE);
        System.out.println(gps_ground!=null&&!list_noise.isEmpty()&&!list_fly.isEmpty());
        if(gps_ground!=null&&!list_noise.isEmpty()&&!list_fly.isEmpty()){
            System.out.println("***********开始执行时间同步****************");
            sync(gps_ground,ori,freq);
            System.out.println("***********时间同步结束****************");
        }

        return res;
    }



    @RequestMapping("/uploadFly")
    public String uploadFly ( @RequestParam("file") MultipartFile file ) throws Exception {
        noise_fly.clear();
        System.out.println("上传飞参数据:"+file.getOriginalFilename());
        String res=ControlUtil.uploadFile(file,FLY_TYPE);
        System.out.println(gps_ground!=null&&!list_noise.isEmpty()&&!list_fly.isEmpty());
//        if(gps_ground!=null&&!list_noise.isEmpty()&&!list_fly.isEmpty()){
//            System.out.println("***********开始执行时间同步****************");
//            sync(gps_ground,ori,freq);
//            System.out.println("***********时间同步结束****************");
//        }
        return res;
    }
}
